package com.shell.governor.modules.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.file.po.PubFile;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.file.service.PubFileUploadService;
import com.shell.bee.file.utils.FileHelper;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.bee.report.excel.WordParser;
import com.shell.governor.modules.dao.PubProtocolDao;
import com.shell.governor.modules.po.PubProtocol;
import com.shell.governor.modules.service.PubProtocolFinService;
import com.shell.governor.modules.service.PubProtocolService;
import com.shell.governor.organization.po.PubOrgan;
import com.shell.governor.organization.service.PubOrganService;

import tk.mybatis.mapper.entity.Example;

@Service
public class PubProtocolServiceImpl extends PersistentServiceImpl<PubProtocol> implements PubProtocolService {
	@Resource(name = "${file.updownload.service}")
	private PubFileUploadService pubFileUploadService;
	@Autowired
	private PubProtocolDao pubProtocolDao;
	@Autowired
	private PubProtocolFinService pubProtocolFinService;
	@Autowired
	private PubOrganService pubOrganService;
	@Autowired
	private PubFileService pubFileService;
	private String defaultGroup = "PT_0";

	@Pagination
	public List<PubProtocol> selectProtocolListByPage(PageLimit limit, PubProtocol pubProtocol, boolean finFlag) {
		Example example = new Example(PubProtocol.class);
		example.selectProperties(new String[] { "protoId", "protoName", "protoType" });
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtil.emptyAndNull(pubProtocol.getProtoName())) {
			criteria.andLike("protoName", StringUtil.getParameterLike(pubProtocol.getProtoName()));
		}
		if (!StringUtil.emptyAndNull(pubProtocol.getProtoType())) {
			criteria.andEqualTo("protoType", pubProtocol.getProtoType());
		}
		if (finFlag) {
			criteria.andNotEqualTo("grpId", this.defaultGroup);
		} else {
			criteria.andEqualTo("grpId", this.defaultGroup);
		}
		criteria.andEqualTo("creatorId", pubProtocol.getCreatorId());
		return selectByExample(example);
	}

	public Map<String, Object> uploadProtocol(MultipartFile file, String fileType, String savePath) throws Exception {
		String content = WordParser.wordToHtml(file.getInputStream(), file.getOriginalFilename(), "");
		Map<String, Object> result = new HashMap();
		if (WordParser.RESULT.UNEXIST.toString().equals(content)) {
			result.put("type", "error");
			result.put("msg", "����������");
		} else if (WordParser.RESULT.FILEERROR.toString().equals(content)) {
			result.put("type", "error");
			result.put("msg", "������������");
		} else if (WordParser.RESULT.EXCEPTION.toString().equals(content)) {
			result.put("type", "error");
			result.put("msg", "������������");
		} else {
			RetMsg retMsg = this.pubFileUploadService.saveFile(file, fileType, savePath, "");
			result.put("type", retMsg.getType());
			result.put("show", Boolean.valueOf(retMsg.isShow()));
			result.put("msg", retMsg.getMsg());
			result.put("bean", retMsg.getBean());
			result.put("content", content);
		}
		return result;
	}

	public int saveOrdUploadProtocol(PubProtocol protocol, String fileIds, String finIds) {
		if (StringUtil.emptyAndNull(protocol.getGrpId())) {
			protocol.setGrpId(this.defaultGroup);
		}
		if (StringUtil.emptyAndNull(protocol.getProtoId())) {
			protocol.setProtoId(SystemUtil.getUUID());
			protocol.setProtoState("1");

			FileHelper.setRelateId(fileIds, protocol.getProtoId(), protocol.getProtoType());
			if (protocol.getProtoType().equals("discount_proto")) {
				this.pubProtocolFinService.clearAndSave(protocol.getProtoId(), finIds);
			}
			return save(protocol);
		}
		FileHelper.clearRelateId(protocol.getProtoId(), protocol.getProtoType());
		FileHelper.setRelateId(fileIds, protocol.getProtoId(), protocol.getProtoType());
		if (protocol.getProtoType().equals("discount_proto")) {
			this.pubProtocolFinService.clearAndSave(protocol.getProtoId(), finIds);
		}
		return updateNotNull(protocol);
	}

	public PubProtocol selectProtocolByGpAndType(String finId, String groupId, String protoType) {
		if (StringUtil.emptyAndNull(protoType)) {
			return null;
		}
		if (StringUtil.emptyAndNull(groupId)) {
			groupId = this.defaultGroup;
		}
		List<PubProtocol> protocols = this.pubProtocolDao.selectProtocolByGpAndType(finId, groupId, protoType, "1");
		if ((protocols != null) && (protocols.size() > 0)) {
			return (PubProtocol) protocols.get(0);
		}
		List<PubProtocol> ptProtocol = this.pubProtocolDao.selectProtocolByGpAndType(null, this.defaultGroup, protoType,
				"1");
		if ((ptProtocol != null) && (ptProtocol.size() > 0)) {
			return (PubProtocol) ptProtocol.get(0);
		}
		return null;
	}

	public String selectProtoInvalidKeys(String finId, String grpId, String excuType) {
		return this.pubProtocolDao.selectProtoInvalidKeys(finId, grpId, excuType);
	}

	public String checkFinProto(String protoId, String finIds, String grpId) {
		StringBuilder sb = new StringBuilder();
		if (!StringUtil.emptyAndNull(finIds)) {
			String[] ids = finIds.split(",");
			for (String finId : ids) {
				String pId = this.pubProtocolDao.checkFinProto(protoId, finId, grpId);
				if (!StringUtil.emptyAndNull(pId)) {
					PubOrgan pubOrgan = (PubOrgan) this.pubOrganService.selectByKey(finId);
					sb.append(pubOrgan.getOrganName()).append(",");
				}
			}
		}
		if (sb.length() > 0) {
			return sb.substring(0, sb.length() - 1);
		}
		return null;
	}

	public int delProtocolById(String protoId) {
		this.pubProtocolFinService.clearAndSave(protoId, null);
		return delete(protoId);
	}

	public List<String> selectFinIds(String protoId, String protoType) {
		List<String> list = new ArrayList();
		if ("discount_proto".equals(protoType)) {
			list = this.pubProtocolFinService.selectFinIdsByProtoId(protoId);
		}
		return list;
	}

	public PubProtocol selectAuthProtocol() {
		Example example = new Example(PubProtocol.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("protoType", "authen_proto");
		List<PubProtocol> protocols = selectByExample(example);
		if ((protocols != null) && (protocols.size() > 0)) {
			return (PubProtocol) protocols.get(0);
		}
		return null;
	}

	public Map<String, Object> selectProtocolForDownload(String protoId) {
		Map<String, Object> map = new HashMap();
		PubProtocol pp = (PubProtocol) selectByKey(protoId);
		PubFile pf = new PubFile();
		pf.setFileType(pp.getProtoType());
		pf.setRelateId(pp.getProtoId());
		List<PubFile> list = this.pubFileService.selectFiles(pf);
		if (list.size() > 0) {
			map.put(((PubFile) list.get(0)).getFileName(), pp.getContent());
		}
		return map;
	}
}
